#pragma once
#include <functional>
#include <string>
#include "Protocol.hpp"

using handler_t = std::function<HttpResponse(HttpRequest&)>;
class Parser
{
public:
    Parser(const handler_t& handler):_handler(handler)
    {}

    //返回将结果序列化之后的jsonstr,拿到的是jsonstr
    std::string Parse(std::string& in)
    {
        //所以我们将请求应答封装为一个类，继续模块化解耦
        HttpRequest req;
        if(!req.DeSerialize(in))
        {
            LOG(LogLevel::ERROR) << "HttpRequest DeSerialize Error";
            return "";
        }
        HttpResponse res = _handler(req);
        std::string ret;
        if(!res.Serialize(ret))
        {
            LOG(LogLevel::ERROR) << "HttpResponse Serialize Error";
            return "";
        }
        return ret;
    }

    ~Parser()
    {}


private:
    handler_t _handler;//向上回调的模块
};





